Grokking Oracle Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Oracle Easy
Count Number of Pairs With Absolute Difference K (easy)
Solution: Count Number of Pairs With Absolute Difference K
Degree of an Array (easy)
Solution: Degree of an Array
Find Subsequence of Length K With the Largest Sum (easy)
Solution: Find Subsequence of Length K With the Largest Sum
Rectangle Overlap (easy)
Solution: Rectangle Overlap
Remove All Adjacent Duplicates In String (easy)
Solution: Remove All Adjacent Duplicates In String
Island Perimeter (easy)
Solution: Island Perimeter
Climbing Stairs (easy)
Solution: Climbing Stairs
Move Zeroes (easy)
Solution: Move Zeroes
Excel Sheet Column Title (easy)
Solution: Excel Sheet Column Title
Valid Parentheses (Easy)
Solution: Valid Parentheses
Oracle Medium
Longest Palindromic Substring (medium)
Solution: Longest Palindromic Substring
Single Element in a Sorted Array (medium)
Solution: Single Element in a Sorted Array
Pairs of Songs With Total Durations Divisible by 60 (medium)
Solution: Pairs of Songs With Total Durations Divisible by 60
Search a 2D Matrix II (medium)
Solution: Search a 2D Matrix II
Minimize Maximum of Array (medium)
Solution: Minimize Maximum of Array
Asteroid Collision (medium)
Solution: Asteroid Collision
Combination Sum (medium)
Solution: Combination Sum
Binary Tree Right Side View (medium)
Solution: Binary Tree Right Side View
Sort Colors (medium)
Solution: Sort Colors
Group Anagrams (Medium)
Solution: Group Anagrams
Decode String (Medium)
Solution: Decode String
Number of Islands (medium)
Solution: Number of Islands
Binary Tree Zigzag Level Order Traversal (medium)
Solution: Binary Tree Zigzag Level Order Traversal
Longest Common Subsequence (medium)
Solution: Longest Common Subsequence
Rotting Oranges (medium)
Solution: Rotting Oranges
Meeting Rooms II (medium)
Solution: Meeting Rooms II
Delete Node in a BST (medium)
Solution: Delete Node in a BST
Remove Duplicate Letters (medium)
Solution: Remove Duplicate Letters
Longest Substring Without Repeating Characters (medium)
Solution: Longest Substring Without Repeating Characters (medium)
Search in Rotated Array (medium)
Solution: Search in Rotated Array
Spiral Matrix (medium)
Solution: Spiral Matrix
Container With Most Water (Medium)
Solution: Container With Most Water
3Sum (medium)
Solution: 3Sum
Simplify Path (medium)
Solution: Simplify Path
Next Permutation (medium)
Solution: Next Permutation
Basic Calculator II (medium)
Solution: Basic Calculator II
Merge Intervals (medium)
Solution: Merge Intervals
Restore IP Addresses (medium)
Solution: Restore IP Addresses
Longest Increasing Subsequence (medium)
Solution: Longest Increasing Subsequence
Validate Binary Search Tree (medium)
Solution: Validate Binary Search Tree
Oracle Hard
Integer to English Words (hard)
Solution: Integer to English Words
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Merge K Sorted Lists (hard)
Solution: Merge K Sorted Lists
Max Points on a Line (hard)
Solution: Max Points on a Line
Sudoku Solver (hard)
Solution: Sudoku Solver
Longest Valid Parentheses (hard)
Solution: Longest Valid Parentheses
Remove Nth Node From End of List (Medium)
Solution: Remove Nth Node From End of List
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Solution: Longest Valid Parentheses

Problem Statement

You are given a string containing just the characters '(' and ')'. Your task is to find the length of the longest valid (well-formed) parentheses substring.

Example 1:

  • Input: "(())"
  • Expected Output: 4
  • Justification: The entire string is a valid parentheses substring.

Example 2:

  • Input: ")()())"
  • Expected Output: 4
  • Justification: The longest valid parentheses substring is "()()".

Example 3:

  • Input: "(()"
  • Expected Output: 2
  • Justification: The longest valid parentheses substring is "()".

Constraints:

  • 0 <= s.length <= 3 * 104
  • s[i] is '(', or ')'.

Solution

  • Understanding the Problem:

    • The problem involves finding the longest sequence of valid parentheses.
    • A stack data structure can be used to keep track of the indices of the invalid parentheses.
  • Approach:

    • Initialize a stack and push -1 onto it as a marker for the base.
    • Iterate through the string and for each character:
      • If it is '(', push its index onto the stack.
      • If it is ')', pop an element from the stack.
        • If the stack is not empty, calculate the length of the valid parentheses substring by subtracting the current index with the top of the stack.
        • If the stack is empty, push the current index onto the stack to serve as the new base marker.
  • Why This Approach Will Work:

    • The stack keeps track of the indices of invalid parentheses, allowing us to easily calculate the length of valid substrings.
    • By continuously calculating the length and updating the maximum length, we ensure finding the longest valid substring.

Algorithm Walkthrough

Consider the input ")()())":

  • Initialize stack with -1: stack = [-1]
  • Iterate through the string:
    • At index 0: ')'
      • Pop from stack: stack = []
      • Stack is empty, push index 0 onto stack: stack = [0]
    • At index 1: '('
      • Push index 1 onto stack: stack = [0, 1]
    • At index 2: ')'
      • Pop from stack: stack = [0]
      • Calculate length: 2 - 0 = 2
    • At index 3: '('
      • Push index 3 onto stack: stack = [0, 3]
    • At index 4: ')'
      • Pop from stack: stack = [0]
      • Calculate length: 4 - 0 = 4
    • At index 5: ')'
      • Pop from stack: stack = []
      • Stack is empty, push index 5 onto stack: stack = [5]
  • The longest valid parentheses substring is of length 4.

Code

Java

. . . .

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the input string. The algorithm traverses the string once.
  • Space Complexity: O(n), where n is the length of the input string. In the worst case, the stack will store all characters of the string.
Longest Valid Parentheses (hard)
Remove Nth Node From End of List (Medium)
Mark as Completed

On this page

Problem Statement

Solution

Algorithm Walkthrough

Code

Complexity Analysis